<?php
class Item extends CActiveRecord
{

    public $uploadedFile;

	/**
	 * Returns the static model of the specified AR class.
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'metadata';
	}
	
	public function rules()
	{
		return array(
			array('guid, hash, protected_by_hash, start_time, end_time, symbolic_name, description, email_address, file_path, size, object_type_ID', 'required', 'on'=>'create'),
			array('guid, hash', 'length', 'min'=>32, 'max'=>32, 'on'=>'create'),
			array('start_time, end_time', 'date', 'format'=>'yyyy-MM-ss hh:mm:ss', 'on'=>'create'),
			array('symbolic_name, email_address', 'length', 'max'=>50, 'on'=>'create'),
			array('description', 'length', 'max'=>1000, 'on'=>'create'),
			array('file_path', 'length', 'max'=>100, 'on'=>'create'),
			array('object_type_ID', 'unique', 'criteria' => array('select'=>ObjectType::model()->tableName(),
															'condition' => 'object_type_ID=:object_type_ID',
															'params'=>array(':object_type_ID'=>$this->object_type_ID)), 'on'=>'create'
															),
			array('email_address', 'email', 'on'=>'create'),
			array('uploadedFile', 'file', 'on'=>'create'),
			array('protected_by_hash', 'boolean', 'on'=>'create'),
		);
	}
	
	public function relations()
	{
		return array(
			'objectType'=>array(self::BELONGS_TO, 'ObjectType', 'object_type_ID'),
		);
	}
	
	public function getLatestVersion($guid)
	{
		$record=$this->findBySql("SELECT * FROM ".$this->tableName()." WHERE version= (SELECT max(version) FROM ".$this->tableName()." WHERE guid='".$guid."')");
		return $record;
	}
	
	public function getPrimaryKey()
	{
		return array(
			'guid'=>$this->guid,
			'version'=>$this->version,
			'protected_by_hash'=>$this->protected_by_hash,
		);
	}
}
?>
